/***************************************************************************
 *   Copyright (C) 2009 by Jason Pullara                                   *
 *   lordkat@gmail.com                                                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "common.h"

using namespace std;

string sanitize(string sInput) {
	if (!sInput.empty()) {
		for (int i = 0; i < sInput.length(); i++) {
			if ((!isalnum(sInput[i]) && !isspace(sInput[i])) || sInput[i] == '\r' || sInput[i] == '\n') {
				sInput.erase(i, 1);
				i--;
			}
		}
	}
	return sInput;
}

string lowercase(string sInput) {
	if (!sInput.empty()) {
		for (int i = 0; i < sInput.length(); i++)
			sInput[i] = tolower(sInput[i]);
	}
	return sInput;
}

string stripBreak(string sInput) {
	if (!sInput.empty()) {
		for (int i = 0; i < sInput.length(); i++) {
			if (sInput[i] == '\r' || sInput[i] == '\n') {
				sInput.erase(i, 1);
				i--;
			}
		}
	}
	return sInput;
}

string stripSpace(string sInput) {
	if (!sInput.empty()) {
		for (int i = 0; i < sInput.length(); i++) {
			if (isspace(sInput[i])) {
				sInput.erase(i, 1);
				i--;
			}
		}
	}
	return sInput;
}

string itoa(int value, int base) {
	enum { kMaxDigits = 35 };
	string buf;
	buf.reserve( kMaxDigits ); // Pre-allocate enough space.
	// check that the base if valid
	if (base < 2 || base > 16) return buf;
	int quotient = value;
	// Translating number to string with base:
	do {
		buf += "0123456789abcdef"[std::abs(quotient % base)];
		quotient /= base;
	} while (quotient);
	// Append the negative sign for base 10
	if (value < 0 && base == 10) buf += '-';
	std::reverse(buf.begin(), buf.end());
	return buf;
}

string ltoa(long value, int base) {
	enum { kMaxDigits = 35 };
	string buf;
	buf.reserve( kMaxDigits ); // Pre-allocate enough space.
	// check that the base if valid
	if (base < 2 || base > 16) return buf;
	int quotient = value;
	// Translating number to string with base:
	do {
		buf += "0123456789abcdef"[std::abs(quotient % base)];
		quotient /= base;
	} while (quotient);
	// Append the negative sign for base 10
	if (value < 0 && base == 10) buf += '-';
	std::reverse(buf.begin(), buf.end());
	return buf;
}

string uitoa(unsigned int value, int base) {
	enum { kMaxDigits = 35 };
	string buf;
	buf.reserve( kMaxDigits ); // Pre-allocate enough space.
	// check that the base if valid
	if (base < 2 || base > 16) return buf;
	int quotient = value;
	// Translating number to string with base:
	do {
		buf += "0123456789abcdef"[std::abs(quotient % base)];
		quotient /= base;
	} while (quotient);
	// Append the negative sign for base 10
	if (value < 0 && base == 10) buf += '-';
	std::reverse(buf.begin(), buf.end());
	return buf;
}

string ultoa(unsigned long value, int base) {
	enum { kMaxDigits = 35 };
	string buf;
	buf.reserve( kMaxDigits ); // Pre-allocate enough space.
	// check that the base if valid
	if (base < 2 || base > 16) return buf;
	int quotient = value;
	// Translating number to string with base:
	do {
		buf += "0123456789abcdef"[std::abs(quotient % base)];
		quotient /= base;
	} while (quotient);
	// Append the negative sign for base 10
	if (value < 0 && base == 10) buf += '-';
	std::reverse(buf.begin(), buf.end());
	return buf;
}